package com.topboya.linker.ui.presenter;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.topboya.linker.AppConst;
import com.topboya.linker.R;
import com.topboya.linker.api.ApiRetrofit;
import com.topboya.linker.exception.ServerException;
import com.topboya.linker.model.response.CompanyUserListReponse;
import com.topboya.linker.model.response.FollowUserByOfficeResponse;
import com.topboya.linker.model.response.FollowUserLateResponse;
import com.topboya.linker.ui.activity.MainActivity;
import com.topboya.linker.ui.base.Presenter.BasePresenter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.view.FollowUserView;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class FollowUserPrensenter extends BasePresenter<FollowUserView> {


    private static final String TAG = "FollowUserPrensenter-";
    public FollowUserPrensenter(BaseActivity context) {
        super(context);

    }

    public void getFollowUserByOffice(String officeId){

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().getFollowUserByOffice(officeId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(followuserbyoffice -> {
                    String success = followuserbyoffice.getMeta().getSuccess();
                    mContext.hideWaitingDialog();
                    if (null != success && Boolean.parseBoolean(success)) {

                        List<FollowUserByOfficeResponse.SameDepartList> sameDepartLists = followuserbyoffice.getData();

                        getView().getFollowUserByOffice(sameDepartLists);


                    }
                }, this::loginError);
    }
    public void getCompanyUserList(){

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().getCompanyUserList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(companyUserListReponse -> {
                    String success = companyUserListReponse.getMeta().getSuccess();
                    mContext.hideWaitingDialog();
                    if (null != success && Boolean.parseBoolean(success)) {

                        List<CompanyUserListReponse.Data> companyUserLists = companyUserListReponse.getData();

                        getView().getCompanyUserList(companyUserLists);


                    }
                }, this::loginError);
    }
    public void getFollowUser(String type){

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().getFollowUser(type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(followuserlateresponse -> {
                    String success = followuserlateresponse.getMeta().getSuccess();
                    String message = followuserlateresponse.getMeta().getMessage();
                    mContext.hideWaitingDialog();
                    if (null != success && Boolean.parseBoolean(success)) {

                        List<FollowUserLateResponse.LateUser> lateUserlist = followuserlateresponse.getData();

                        getView().getFollowUser(lateUserlist);


                    }
                }, this::loginError);
    }

    private void loginError(Throwable throwable) {

        LogUtils.v(throwable.getLocalizedMessage());
//        UIUtils.showToast(throwable.getLocalizedMessage());
        LogUtils.v(TAG + "loginError:" + throwable.getLocalizedMessage());
        mContext.hideWaitingDialog();
    }

}
